#ifndef _WORK_H_
#define _WORK_H_

#include "ddl.h"
#include "gpio.h"
#include "adc.h"

#define LED8_PORT					GpioPortC
#define LED8_PIN					GpioPin13
#define EMS_SW_EN_PORT		GpioPortC
#define EMS_SW_EN_PIN			GpioPin14
#define LED7_PORT					GpioPortB
#define LED7_PIN					GpioPin5
#define LED6_PORT					GpioPortB
#define LED6_PIN					GpioPin4
#define EMS_VCC_PWM_PORT	GpioPortD
#define EMS_VCC_PWM_PIN		GpioPin1
#define EMS_VCC_EN_PORT		GpioPortA
#define EMS_VCC_EN_PIN		GpioPin0
#define GPIO_IPL_PORT			GpioPortA
#define GPIO_IPL_PIN			GpioPin4
#define ISET_PORT					GpioPortA
#define ISET_PIN					GpioPin5
#define BOOST_Control_PORT			GpioPortA
#define BOOST_Control_PIN				GpioPin6
#define GPIO_Control_Ice_PORT		GpioPortA
#define GPIO_Control_Ice_PIN		GpioPin7
#define ADC_300V_PORT			GpioPortB
#define ADC_300V_PIN			GpioPin0
#define ADC_24V_PORT			GpioPortA
#define ADC_24V_PIN				GpioPin3
#define FAN_PORT					GpioPortA
#define FAN_PIN						GpioPin2
#define FAN_CHECK_PORT		GpioPortA
#define FAN_CHECK_PIN			GpioPin1
#define NTC_FAN_PORT			GpioPortB
#define NTC_FAN_PIN				GpioPin1
#define K2_PORT						GpioPortB
#define K2_PIN						GpioPin2
#define LED1_PORT					GpioPortB
#define LED1_PIN					GpioPin12
#define EMS_VCC_ADC_PORT	GpioPortB
#define EMS_VCC_ADC_PIN		GpioPin14
#define EMS_ISEN_PORT			GpioPortB
#define EMS_ISEN_PIN			GpioPin15
#define LED2_PORT					GpioPortB
#define LED2_PIN					GpioPin13
#define TX_PORT						GpioPortA
#define TX_PIN						GpioPin9
#define RX_PORT						GpioPortA
#define RX_PIN						GpioPin10
#define IIC_SCL_PORT			GpioPortA
#define IIC_SCL_PIN				GpioPin11
#define IIC_SDA_PORT			GpioPortA
#define IIC_SDA_PIN				GpioPin12
#define LED3_PORT					GpioPortD
#define LED3_PIN					GpioPin6
#define LED5_PORT					GpioPortB
#define LED5_PIN					GpioPin3
#define BUZZER_PORT				GpioPortD
#define BUZZER_PIN				GpioPin7
#define LED4_PORT					GpioPortA
#define LED4_PIN					GpioPin8
#define K1_PORT						GpioPortA
#define K1_PIN						GpioPin15
#define K3_PORT						GpioPortB
#define K3_PIN						GpioPin10
#define NTC_CAP_PORT			GpioPortB
#define NTC_CAP_PIN				GpioPin11
#define Touch2_PORT				GpioPortB
#define Touch2_PIN				GpioPin6
#define EMS_PWM_P_PORT		GpioPortB
#define EMS_PWM_P_PIN			GpioPin7
#define Touch1_PORT				GpioPortD
#define Touch1_PIN				GpioPin3
#define LED_EN_PORT				GpioPortB
#define LED_EN_PIN				GpioPin8
#define EMS_PWM_N_PORT		GpioPortB
#define EMS_PWM_N_PIN			GpioPin9

#define EMS_VCC_PWM_OFF		Gpio_ClrIO(EMS_VCC_PWM_PORT,EMS_VCC_PWM_PIN)
#define EMS_SW_EN_ON			Gpio_SetIO(EMS_SW_EN_PORT,EMS_SW_EN_PIN)
#define EMS_SW_EN_OFF			Gpio_ClrIO(EMS_SW_EN_PORT,EMS_SW_EN_PIN)
#define LED7_OFF					Gpio_SetIO(LED7_PORT,LED7_PIN)
#define LED7_ON						Gpio_ClrIO(LED7_PORT,LED7_PIN)
#define LED6_OFF					Gpio_SetIO(LED6_PORT,LED6_PIN)
#define LED6_ON						Gpio_ClrIO(LED6_PORT,LED6_PIN)
#define LED8_OFF					Gpio_SetIO(LED8_PORT,LED8_PIN)
#define LED8_ON						Gpio_ClrIO(LED8_PORT,LED8_PIN)
#define EMS_VCC_EN_ON			Gpio_SetIO(EMS_VCC_EN_PORT,EMS_VCC_EN_PIN)
#define EMS_VCC_EN_OFF		Gpio_ClrIO(EMS_VCC_EN_PORT,EMS_VCC_EN_PIN)
#define GPIO_IPL_ON				Gpio_SetIO(GPIO_IPL_PORT,GPIO_IPL_PIN)
#define GPIO_IPL_OFF			Gpio_ClrIO(GPIO_IPL_PORT,GPIO_IPL_PIN)
#define BOOST_Control_OFF			Gpio_ClrIO(BOOST_Control_PORT,BOOST_Control_PIN)
#define GPIO_Control_Ice_ON		Gpio_SetIO(GPIO_Control_Ice_PORT,GPIO_Control_Ice_PIN)
#define GPIO_Control_Ice_OFF	Gpio_ClrIO(GPIO_Control_Ice_PORT,GPIO_Control_Ice_PIN)
#define FAN_ON						Gpio_SetIO(FAN_PORT,FAN_PIN)
#define FAN_OFF						Gpio_ClrIO(FAN_PORT,FAN_PIN)
#define FAN_CHECK					Gpio_GetInputIO(FAN_CHECK_PORT,FAN_CHECK_PIN)
#define K2								Gpio_GetInputIO(K2_PORT,K2_PIN)
#define LED1_OFF					Gpio_SetIO(LED1_PORT,LED1_PIN)
#define LED1_ON						Gpio_ClrIO(LED1_PORT,LED1_PIN)
#define EMS_ISEN					Gpio_GetInputIO(EMS_ISEN_PORT,EMS_ISEN_PIN)
#define LED2_OFF					Gpio_SetIO(LED2_PORT,LED2_PIN)
#define LED2_ON						Gpio_ClrIO(LED2_PORT,LED2_PIN)
#define LED3_OFF					Gpio_SetIO(LED3_PORT,LED3_PIN)
#define LED3_ON						Gpio_ClrIO(LED3_PORT,LED3_PIN)
#define LED5_OFF					Gpio_SetIO(LED5_PORT,LED5_PIN)
#define LED5_ON						Gpio_ClrIO(LED5_PORT,LED5_PIN)
#define BUZZER_ON					Gpio_SetIO(BUZZER_PORT,BUZZER_PIN)
#define BUZZER_OFF				Gpio_ClrIO(BUZZER_PORT,BUZZER_PIN)
#define LED4_OFF					Gpio_SetIO(LED4_PORT,LED4_PIN)
#define LED4_ON						Gpio_ClrIO(LED4_PORT,LED4_PIN)
#define K1								Gpio_GetInputIO(K1_PORT,K1_PIN)
#define K3								Gpio_GetInputIO(K3_PORT,K3_PIN)
#define Touch2						Gpio_GetInputIO(Touch2_PORT,Touch2_PIN)
#define EMS_PWM_P_ON			Gpio_SetIO(EMS_PWM_P_PORT,EMS_PWM_P_PIN)
#define EMS_PWM_P_OFF			Gpio_ClrIO(EMS_PWM_P_PORT,EMS_PWM_P_PIN)
#define Touch1_ON					Gpio_SetIO(Touch1_PORT,Touch1_PIN)
#define Touch1_OFF				Gpio_ClrIO(Touch1_PORT,Touch1_PIN)
#define LED_EN_ON					Gpio_SetIO(LED_EN_PORT,LED_EN_PIN)
#define	LED_EN_OFF				Gpio_ClrIO(LED_EN_PORT,LED_EN_PIN)
#define EMS_PWM_N_ON			Gpio_SetIO(EMS_PWM_N_PORT,EMS_PWM_N_PIN)
#define EMS_PWM_N_OFF			Gpio_ClrIO(EMS_PWM_N_PORT,EMS_PWM_N_PIN)

#define ISET_Channel			AdcExInputCH5
#define ADC_300V_Channel	AdcExInputCH8
#define ADC_24V_Channel		AdcExInputCH3
#define NTC_FAN_Channel		AdcExInputCH9
#define NTC_CAP_Channel		AdcExInputCH18
#define EMS_VCC_ADC_Channel 	AdcExInputCH21
#define VCC_Channel				AdcAVccdiv3Input

#define Trigger_Min_Voltage	210

#define Input_Voltage_Offset	15		//���ó�����ѹ��*10
//#define	Input_Voltage_Over	250
//#define Input_Voltage_Under	234		//225
//#define Input_Voltage_Warning	236	//227 == 2.8A 221 == 4A
#define MAX_Current					  2.3		//������������

#define CHARGE_FREQ		120000
#define EMS_VCC_FREQ	20000
#define EMS_FREQ			20000
#define FAN_FREQ			4000

#define Voltage_Offset 3
#define MAX_Duty		48		
#define MIN_Duty		1

enum
{
		Trigger_Start=1,
		Trigger_End=0,
};

enum
{
		Voltage_Over=3,
		Voltage_Normal=2,
		Voltage_Warning=1,
		Voltage_Under=0,
};

enum
{
		EMS_VCC_PWM_Channel,
		BOOST_Control_Channel,
		FAN_Channel,
		BUZZER_Channel,
};
//1=TEMP��2=NTC��4=INPUT��8=FAN��16=CHARGE_SPEED��32=OVER_CURRENT��64=EMSV��128=OVER_VOLTAGE
enum
{
		NO_ERROR=0,
		TEMP_OVER=0x01,
		NTC_ERROR=0x02,
		INPUT_ERROR=0x04,
		FAN_ERROR=0x08,
		CHARGE_SPEED_ERROR=0x10,
		CURRENT_ERROR=0x20,
		EMS_ERROR=0x40,
		VOLTAGE_ERROR=0x80,
};

//enum
//{
//		TEST_MODE,
//		MANUFACTURING_INFORMATION,
//		NTC_TEMP,
//		BATTERY_VOLTGAE,
//		MOTOR_VIBRATE_TEST,
//	
//		LED_IR_TEST,
//		ELECTRODE_HEAD_STATE,
//		ATOMIZE_TEST,
//		EMS_TEST,
//		KEY_TEST,
//	
//		LIGHT_TEST,
//		G_SENSOR_TEST,
//		RGB_SENSOR_CALIBRATION,
//		RGB_SENSOR_TEST,
//		AGING_MODE,
//	
//		LIFE_MODE,
//		BOOTLOADER_MODE,
//		VERSION_VERIFICATION,
//		STANDBY_CURRENT_TEST,
//};

void EMS_HF_Shift(void);
void Work_Driver(void);
void Work_Handler(void);
void Mode_Config(void);
void Trigger_Time(uint16_t us);
void Trigger_Handle(uint16_t duty,uint8_t count);
void Trigger_Error_Detect(void);
void Voltage_Current_Detect(void);
void Voltage_Control(uint16_t Target_Voltage);
void Input_Voltage_Protect(void);
void Work_CountTime(void);
void DEBUG_Handler(void);
void Touch_Detect(void);

#endif
